const { sqlPromise } = require("../db/sqlPromise");
// 导入解析 formdata 格式表单数据的包
const multer = require("multer");
// 导入处理路径的核心模块
const path = require("path");

// 创建 multer 的实例对象，通过 dest 属性指定文件的存放路径
const upload = multer({ dest: path.join(__dirname, "../uploads") });

exports.addArticle = async (req, res) => {
  console.log(req.body); // 文本类型的数据
  console.log("--------分割线----------");
  console.log(req.file); // 文件类型的数据
  if (!req.file || req.file.fieldname !== "cover_img")
    return res.send({ status: 1, msg: "文章封面是必选参数!" });

  const articleInfo = {
    // 标题、内容、状态、所属的分类Id
    ...req.body,
    // 文章封面在服务器端的存放路径
    cover_img: path.join("/uploads", req.file.filename),
    // 文章发布时间
    pub_date: new Date(),
    // 文章作者的Id
    author_id: req.user.id,
  };
  console.log(new Date());
  const sql = `insert into ev_articles set ?`;
  let results = await sqlPromise(sql, articleInfo);
  console.log(results);
  if (results.affectedRows !== 1)
    return res.send({ status: 1, msg: "发布文章失败" });
  res.send({
    status: 0,
    msg: "发布文章成功",
  });
};

exports.getArticleList = async (req, res) => {
  let config = "articles.is_delete=0";
  if (req.query.cate_id)
    config += ` and articles.cate_id = ${req.query.cate_id}`;
  if (req.query.state) config += ` and articles.state = ${req.query.state}`;
  // const sql = `select id,title,pub_date,state,cate_id from ev_articles where ${config}`;

  let sql = `select articles.id, articles.title, articles.pub_date, articles.state,cate.name as cate_name from ev_articles articles left join ev_article_cate cate on cate.id=articles.cate_id where ${config}`;
  let results = await sqlPromise(sql, config);
  if (!results) return res.send({ status: 1, msg: "获取文章失败" });
  res.send({
    status: 0,
    msg: "获取文章成功",
    data: results,
  });
};

exports.deleteArticleById = async (req, res) => {
  let del_sql = "update ev_articles set is_delete=1 where id=?";
  let results = await sqlPromise(del_sql, [req.params.id]);
  if (results.affectedRows !== 1)
    return res.send({ status: 1, msg: "删除文章失败！" });
  res.send({
    status: 0,
    msg: "删除文章成功！",
  });
};

exports.getArticleById = async (req, res) => {
  const sql = "select * from ev_articles where id=?";
  let results = await sqlPromise(sql, [req.params.id]);
  if (results.length == 0) return res.send({ status: 1, msg: "获取文章失败" });
  res.send({
    status: 0,
    msg: "获取文章成功",
    data: results,
  });
};

exports.updateArticleById = async (req, res) => {
  if (!req.file || req.file.fieldname !== "cover_img")
    return res.send({ status: 1, msg: "文章封面是必选参数!" });

  const articleInfo = {
    // 标题、内容、状态、所属的分类Id
    ...req.body,
    // 文章封面在服务器端的存放路径
    cover_img: path.join("/uploads", req.file.filename),
    // 文章发布时间
    pub_date: new Date(),
    // 文章作者的Id
    author_id: req.user.id,
  };

  const update_sql = "update ev_articles set ? where id=?";
  let updateRes = await sqlPromise(update_sql, [articleInfo, req.body.id]);
  if (updateRes.affectedRows !== 1)
    return res.send({ status: 1, msg: "更新文章失败" });
  res.send({
    status: 0,
    msg: "更新文章成功",
  });
};
